package com.education.util;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.auth.sts.AssumeRoleRequest;
import com.aliyuncs.auth.sts.AssumeRoleResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.http.ProtocolType;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;

public class AliStsVideo {
	// 目前只有"cn-hangzhou"这个region可用, 不要使用填写其他region的值
    public static final String REGION_CN_HANGZHOU = "cn-shanghai";
    // 当前 STS API 版本
    public static final String STS_API_VERSION = "2015-04-01";
    public static final String accessKeyId = "LTAI4GF8v7x7qaKTgqZXD3rw";
    public static final String accessKeySecret = "L7qdgVEn6ZevPLvWScdDMYcs83Igm5";
    public static final String roleArn = "acs:ram::1640133191278035:role/hykj";
    public static final String roleSessionName = "videoUpload";
    public static final String policy = "{\n" +
            "  \"Version\": \"1\",\n" +
            "  \"Statement\": [\n" +
            "    {\n" +
            "      \"Action\": \"vod:*\",\n" +
            "      \"Resource\": \"*\",\n" +
            "      \"Effect\": \"Allow\"\n" +
            "    }\n" +
            "  ]\n" +
            "}";
    // 具体规则请参考API文档中的格式要求
   public static JSONObject assumeRole() throws ClientException {
	   JSONObject object=new JSONObject();
        try {
        	 ProtocolType protocolType = ProtocolType.HTTPS;
            // 创建一个 Aliyun Acs Client, 用于发起 OpenAPI 请求
            IClientProfile profile = DefaultProfile.getProfile(REGION_CN_HANGZHOU, accessKeyId, accessKeySecret);
            DefaultAcsClient client = new DefaultAcsClient(profile);
            // 创建一个 AssumeRoleRequest 并设置请求参数
            final AssumeRoleRequest request = new AssumeRoleRequest();
            request.setVersion(STS_API_VERSION);
            request.setMethod(MethodType.POST);
            request.setProtocol(protocolType);
            request.setRoleArn(roleArn);
            request.setRoleSessionName(roleSessionName);
//            long time = 3600;//15m-60m
//            request.setDurationSeconds(time);  
            request.setPolicy(policy);
            // 发起请求，并得到response
            final AssumeRoleResponse response = client.getAcsResponse(request);
            object.put("Expiration", response.getCredentials().getExpiration());
            object.put("AccessKeyId",response.getCredentials().getAccessKeyId() );
            object.put("AccessKeySecret", response.getCredentials().getAccessKeySecret());
            object.put("SecurityToken",response.getCredentials().getSecurityToken() );
            return object;
        } catch (ClientException e) {
            throw e;
        }
    }
  
}
